#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h> 
#include <string.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <errno.h>


#include "structure.h"
#include "toolkit.h"
#include "config.h"


#define PATH "./souris.jpg"


struct database db; 

int main(int argc, char *argv[]){

  struct stat stats;
  if (stat(PATH, &stats) == -1) {
    fprintf(stderr,"Cannot stat file: %s\n",PATH);
  }
  int size = stats.st_size;


  char *buffer = malloc(size*sizeof(char));
  
  bzero(buffer,size*sizeof(char));
  
  args* arglist = malloc(sizeof(args));
  arglist->argv = argv;
  arglist->portno = NO_PORT_TRACKER;
  arglist->IP = IP_TRACKER;
  arglist->func = print;
  arglist->size = size;
  
  FILE* fd = fopen(PATH,"rb");
  int rd = fread(buffer, sizeof(char), size, fd);

  printf("size = %d\nrd = %d\n%s\n",size, rd, buffer);

  arglist->buffer = buffer;

  pthread_t Thread_connection;
  
  if (pthread_create(&Thread_connection,NULL,(void*)send_msg,arglist) !=0)
    fprintf(stderr,"ERROR creating thread.\n");
  
  if (pthread_join(Thread_connection, NULL) !=0)
    fprintf(stderr,"ERROR executing thread.\n");

  fclose(fd);
  free(buffer);

  
  return 0;
}
